package com.ysd.lis.service.qi.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.common.dto.ColumnAndCommentDto;
import com.ysd.lis.entity.qi.QiIndexBusinessModule;
import com.ysd.lis.mapper.qi.QiIndexBusinessModuleMapper;
import com.ysd.lis.mapper.sys.SysBasDictTableMapper;
import com.ysd.lis.service.qi.QiIndexBusinessModuleService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 组件模块对照表 服务实现类
 * </p>
 *
 * @author WH
 * @since 2024-07-10
 */
@Service
public class QiIndexBusinessModuleServiceImpl extends ServiceImpl<QiIndexBusinessModuleMapper, QiIndexBusinessModule> implements QiIndexBusinessModuleService {

    @Autowired
    QiIndexBusinessModuleMapper qiIndexBusinessModuleMapper;
    @Autowired
    SysBasDictTableMapper sysBasDictTableMapper;
    @Override
    public Result getBusinessModuleAndData(QiIndexBusinessModule pageParam) {
        MPJLambdaWrapper<QiIndexBusinessModule> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(QiIndexBusinessModule.class)
                .eq(QiIndexBusinessModule::getModuleType,pageParam.getModuleType())
                .eq(QiIndexBusinessModule::getDelFlag,0);
        List<QiIndexBusinessModule> moduleList = qiIndexBusinessModuleMapper.selectList(queryWrapper);
        if(ToolsUtils.isNotEmpty(moduleList)){
            QiIndexBusinessModule qiIndexBusinessModule = moduleList.get(0);
            Map<String,Object> returnMap = new HashMap<>();
            List<String> columNameList = new ArrayList<>();
            String outField = qiIndexBusinessModule.getOutField();
            columNameList.add(outField);
            String showField = qiIndexBusinessModule.getShowField();
            columNameList.add(showField);
            String showFields = qiIndexBusinessModule.getShowFields();
            if(ToolsUtils.isNotEmpty(showFields)){
                String[] split = showFields.split(",");
                for(String s: split){
                    columNameList.add(s);
                }
            }

            List<ColumnAndCommentDto> ColumnAndComments = sysBasDictTableMapper.findColumnsByTableName(qiIndexBusinessModule.getTableName());
            List<ColumnAndCommentDto> tableDataList = new ArrayList<>();
            for(String columName : columNameList){
                Optional<ColumnAndCommentDto> first = ColumnAndComments.stream().filter(a -> a.getColumnName().equals(columName)).findFirst();
                if(first.isPresent()){
                    ColumnAndCommentDto columnAndCommentDto = first.get();
                    tableDataList.add(columnAndCommentDto);
                }
            }

            returnMap.put("tableFieldList",tableDataList);

            List<Map<String, Object>> transmitTodoData = sysBasDictTableMapper.getTransmitTodoDataByBusiness(qiIndexBusinessModule.getTableName(),qiIndexBusinessModule.getOutField(),pageParam.getIndexCode(),pageParam.getItemCode(),pageParam.getMajorCode(),pageParam.getModuleType(),columNameList,pageParam.getKeywords());
            returnMap.put("tableDataList",transmitTodoData);
            return Result.succ(1, "查询成功", returnMap);

        }else {
            return Result.succ(1, "查询成功", null);
        }

    }

    @Override
    public Result getMajorModuleAndData(QiIndexBusinessModule pageParam) {
        MPJLambdaWrapper<QiIndexBusinessModule> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(QiIndexBusinessModule.class)
                .eq(QiIndexBusinessModule::getModuleType,pageParam.getModuleType())
                .eq(QiIndexBusinessModule::getDelFlag,0);
        List<QiIndexBusinessModule> moduleList = qiIndexBusinessModuleMapper.selectList(queryWrapper);
        if(ToolsUtils.isNotEmpty(moduleList)){
            QiIndexBusinessModule qiIndexBusinessModule = moduleList.get(0);
            Map<String,Object> returnMap = new HashMap<>();
            List<String> columNameList = new ArrayList<>();
            String outField = qiIndexBusinessModule.getOutField();
            columNameList.add(outField);
            String showField = qiIndexBusinessModule.getShowField();
            columNameList.add(showField);
            String showFields = qiIndexBusinessModule.getShowFields();
            if(ToolsUtils.isNotEmpty(showFields)){
                String[] split = showFields.split(",");
                for(String s: split){
                    columNameList.add(s);
                }
            }

            List<ColumnAndCommentDto> ColumnAndComments = sysBasDictTableMapper.findColumnsByTableName(qiIndexBusinessModule.getTableName());
            List<ColumnAndCommentDto> tableDataList = new ArrayList<>();
            for(String columName : columNameList){
                Optional<ColumnAndCommentDto> first = ColumnAndComments.stream().filter(a -> a.getColumnName().equals(columName)).findFirst();
                if(first.isPresent()){
                    ColumnAndCommentDto columnAndCommentDto = first.get();
                    tableDataList.add(columnAndCommentDto);
                }
            }

            returnMap.put("tableFieldList",tableDataList);

            List<Map<String, Object>> transmitTodoData = sysBasDictTableMapper.getTransmitTodoDataByMajorBusiness(qiIndexBusinessModule.getTableName(),qiIndexBusinessModule.getOutField(),pageParam.getModuleType(),columNameList,pageParam.getKeywords());
            returnMap.put("tableDataList",transmitTodoData);
            return Result.succ(1, "查询成功", returnMap);

        }else {
            return Result.succ(1, "查询成功", null);
        }
    }
}
